package netty.nio.zerocopy;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author: zhangrongqing
 * @Date: 2020/1/13 10:50
 */
public class NewIoServer {
    public static void main(String[] args) throws  Exception{
        InetSocketAddress inetSocketAddress = new InetSocketAddress(9090);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(inetSocketAddress);

        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);

        while (true){
            SocketChannel socketChannel = serverSocketChannel.accept();
            int read = socketChannel.read(byteBuffer);
            if(read > 0){
                try {
                    //倒带，pos = 0 ，remark作废（即=-1）
                    byteBuffer.rewind();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                break;
            }
        }
    }
}
